package com.hu.bgp.client.widgets;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.hu.bgp.shared.ClientGameType;

public abstract class GameTypeTable extends HeaderedFlexTable
{
	private static final String GAMETYPE_RADIOGROUP = "gameType";
	private int rows = 1;
	private int columns = 1;
	private ClientGameType selection;

	public GameTypeTable()
	{
		super();
		clear();
		this.getElement().addClassName("bgptable");
	}

	public ClientGameType getSelection()
	{
		return selection;
	}

	public void addGameRow(ClientGameType gameType)
	{
		new GameRow(gameType, createRow(gameType));
	}

	public void clear()
	{
		super.clear();
		rows = 1;
		columns = 1;
		this.setHeaderElement(0, DOM.createDiv());
		for (Label l : createHeader())
		{
			this.setHeaderElement(columns++, l.getElement());
		}
	}

	public abstract Widget[] createRow(ClientGameType gameType);

	public abstract Label[] createHeader();

	protected class GameRow
	{
		private final ClientGameType gameType;
		private final RadioButton rb = new RadioButton(GAMETYPE_RADIOGROUP);

		public GameRow(ClientGameType gameType, Widget[] fields)
		{
			this.gameType = gameType;
			rb.addValueChangeHandler(new ValueChangeHandler<Boolean>()
			{
				public void onValueChange(ValueChangeEvent<Boolean> event)
				{
					selection = GameRow.this.gameType;
				}
			});

			setWidget(rows, 0, rb);
			int rc = 1;
			for (Widget w : fields)
			{
				setWidget(rows, rc++, w);
			}
			rows++;
		}
	}
}
